package com.imis.module.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.imis.base.constant.permissions.PermissionsValueConstant;
import com.imis.base.globle.Result;
import com.imis.module.base.BaseController;
import com.imis.module.system.bus.SysQuartzJobBus;
import com.imis.module.system.model.ro.PagingQueryQuartzJobDTO;
import com.imis.module.system.model.ro.SysQuartzJobAddRO;
import com.imis.module.system.model.ro.SysQuartzJobUpdateRO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 定时任务 前端控制器
 * </p>
 *
 * @author XinLau
 * @since 2020-06-15
 */
@Slf4j
@RestController
@RequestMapping("/sys/quartz")
@Api(tags = {"定时任务"})
@ApiSort(7)
public class SysQuartzJobController extends BaseController<SysQuartzJobBus> {

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value = "分页查询定时任务接口", notes = "分页查询定时任务接口")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_PAGE)
    @ApiOperationSupport(order = 1, author = "XinLau")
    public Result pagingQuerySysQuartzJobListByParameter(PagingQueryQuartzJobDTO pagingQueryQuartzJobDTO) {
        return service.pagingQuerySysQuartzJobListByParameter(pagingQueryQuartzJobDTO);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "定时任务添加接口", notes = "单条添加")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_ADD)
    @ApiOperationSupport(order = 2, author = "XinLau")
    public Result addQuartzJob(@RequestBody @Valid SysQuartzJobAddRO sysQuartzJobAddRo) {
        return service.addQuartzJob(sysQuartzJobAddRo);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务删除接口", notes = "单条删除")
    @RequiresPermissions(value = {PermissionsValueConstant.QUARTZ_PAGE, PermissionsValueConstant.QUARTZ_DELETE}, logical = Logical.AND)
    @ApiImplicitParam(name = "id", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 3, author = "XinLau")
    public Result deleteQuartzJob(@RequestParam(name = "id", required = true) Long id) {
        return service.deleteQuartzJob(id);
    }

    @RequestMapping(value = "/queryQuartzJobById", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务查看接口", notes = "单条查看")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_QUERY_BY_ID)
    @ApiImplicitParam(name = "id", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 4, author = "XinLau")
    public Result queryQuartzJobById(@RequestParam(name = "id", required = true) Long id) {
        return service.queryQuartzJobById(id);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value = "定时任务修改接口", notes = "单条修改")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_UPDATE)
    @ApiOperationSupport(order = 5, author = "XinLau")
    public Result updateQuartzJob(@RequestBody @Valid SysQuartzJobUpdateRO sysQuartzJobUpdateRo) {
        return service.updateQuartzJob(sysQuartzJobUpdateRo);
    }

    @RequestMapping(value = "/scheduler", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务构建接口", notes = "单条构建")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_SCHEDULER)
    @ApiImplicitParam(name = "quartzJobIdentification", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 6, author = "XinLau")
    public Result schedulerQuartzJob(@RequestParam(name = "quartzJobIdentification", required = true) Long quartzJobIdentification) {
        return service.schedulerQuartzJobByQuartzJobIdentification(quartzJobIdentification);
    }

    @RequestMapping(value = "/trigger", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务执行接口", notes = "单条执行")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_TRIGGER)
    @ApiImplicitParam(name = "quartzJobIdentification", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 7, author = "XinLau")
    public Result triggerQuartzJob(@RequestParam(name = "quartzJobIdentification", required = true) Long quartzJobIdentification) {
        return service.triggerQuartzJobByQuartzJobIdentification(quartzJobIdentification);
    }

    @RequestMapping(value = "/pause", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务暂停接口", notes = "单条暂停")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_PAUSE)
    @ApiImplicitParam(name = "quartzJobIdentification", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 8, author = "XinLau")
    public Result pauseQuartzJob(@RequestParam(name = "quartzJobIdentification", required = true) Long quartzJobIdentification) {
        return service.pauseQuartzJobByQuartzJobIdentification(quartzJobIdentification);
    }

    @RequestMapping(value = "/resume", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务恢复接口", notes = "单条恢复")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_RESUME)
    @ApiImplicitParam(name = "quartzJobIdentification", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 9, author = "XinLau")
    public Result resumeQuartzJob(@RequestParam(name = "quartzJobIdentification", required = true) Long quartzJobIdentification) {
        return service.resumeQuartzJobByQuartzJobIdentification(quartzJobIdentification);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务移除接口", notes = "单条移除")
    @RequiresPermissions(PermissionsValueConstant.QUARTZ_REMOVE)
    @ApiImplicitParam(name = "quartzJobIdentification", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 10, author = "XinLau")
    public Result removeQuartzJob(@RequestParam(name = "quartzJobIdentification", required = true) Long quartzJobIdentification) {
        return service.removeQuartzJobByQuartzJobIdentification(quartzJobIdentification);
    }

    @RequestMapping(value = "/getQuartzJobState", method = RequestMethod.GET)
    @ApiOperation(value = "获取定时任务状态", notes = "单条获取")
    @ApiImplicitParam(name = "quartzJobIdentification", value = "定时任务标识", dataType = "Long", dataTypeClass = Long.class, required = true)
    @ApiOperationSupport(order = 11, author = "XinLau")
    public Result getQuartzJobState(@RequestParam(name = "quartzJobIdentification", required = true) Long quartzJobIdentification) {
        return service.getQuartzJobStateByQuartzJobIdentification(quartzJobIdentification);
    }

}